package com.tf.xx.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tf.xx.R;
import com.tf.xx.entity.EduTeacher;
import com.tf.xx.entity.vo.TeacherQuery;
import com.tf.xx.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "edu_teacher", description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Operation(summary = "查询所有讲师")
    @GetMapping("/findAll")
    public  R getAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }


    @Operation(summary = "逻辑删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R deleteTeacher(@PathVariable("id") String id){
        boolean result = eduTeacherService.removeById(id);
        if (!result){
            return R.error();
        }
        return R.ok();
    }

    @Operation(summary = "分页查询讲师")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListTeacher(@PathVariable("page") long page,
                             @PathVariable("limit") long limit){
        Page<EduTeacher> pagelist = eduTeacherService.pageListTeacher(page, limit);
        return R.ok().data("total", pagelist.getTotal()).data("rows", pagelist.getRecords());
    }

    //新增讲师
    @Operation(summary ="新增讲师",description ="新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher){
        boolean flag =eduTeacherService.save(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
//根据id查询,用于信息回显

        @Operation(summary ="根据id査询讲师",description ="根据id查询讲师")
        @GetMapping("/getById/{id}")
        public R getById(@PathVariable String id) {
            EduTeacher teacher = eduTeacherService.getById(id);
            return R.ok().data("item", teacher);
        }

    //修改讲师

    @Operation(summary="修改讲师",description="修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher teacher) {
        boolean flag = eduTeacherService.updateById(teacher);
            if (flag) {
                return R.ok();
            } else {
                return R.error();
            }
        }
    //多条件查询讲师带分页
    @Operation(summary ="多条件查询讲师带分页",description ="多条件查询讲师带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageListTeacherCondition(@PathVariable long page,
                                      @Parameter(name ="page",description ="分页页数",required = true)
                                      @PathVariable("limit") long limit,
                                      @Parameter(name ="limit",description ="每页记录数",required = true)
                                      @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> pageParam= new Page<>(page,limit);
        eduTeacherService.pageQuery(pageParam,teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows",records);
    }


}
